package com.functest.orm

import com.functest.framework.BaseFunctionalTest
import org.testng.annotations.DataProvider
import org.testng.annotations.Test

/**
 * Created with IntelliJ IDEA.
 * User: KovalevAS
 * Date: 10.09.13
 * Time: 17:44
 * To change this template use File | Settings | File Templates.
 */
class EqualsHashcodeTest extends BaseFunctionalTest{


    @DataProvider
    Object [][] data(){
        //[x, y, z, notX]
        [
                [
                        new Condition().with {
                            field = 'field'
                            op = ConditionOp.EQ
                            value = 10
                            it
                        },
                        Condition.of('field', ConditionOp.EQ, 10),
                        Condition.of('field', ConditionOp.EQ, 10),
                        Condition.of('field', ConditionOp.EQ, 12)
                ]
        ]
    }

    @Test(dataProvider = 'data')
    void equalsTest(Object x, Object y, Object z, Object notX) {
        asTest {
            /*equals*/
            asStep("reflexive test") {
                com.functest.framework.Assertions.weakAssertSilently("reflexive test failed", x.equals(x))
            }
            asStep("symmetric test") {
                com.functest.framework.Assertions.weakAssertSilently("symmetric test failed",
                        x.equals(y)
                                && y.equals(x)
                                && !x.equals(notX)
                                && !notX.equals(x))
            }
            asStep("transitive test") {
                com.functest.framework.Assertions.weakAssertSilently("transitive test failed",
                        x.equals(y) && y.equals(z) && x.equals(z))
            }
            asStep("consistent test") {
                1.upto(10) {
                    com.functest.framework.Assertions.weakAssertSilently("consistent test failed", x.equals(y) && !x.equals(notX))
                }
            }
            asStep("null test") {
                com.functest.framework.Assertions.weakAssertSilently("null test failed", !x.equals(null))
            }
            /*hashcode*/
            asStep('checking hashcode for equal objects') {
                com.functest.framework.Assertions.weakAssertSilently('hashcode doesn\'t matches for equal objects', x.hashCode() == y.hashCode())
            }
            asStep('checking hashcode for different objects') {
                com.functest.framework.Assertions.weakAssertSilently('error: hashcode matches for different objects', x.hashCode() != notX.hashCode())
            }
        }
    }
}
